using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GameStateManagement
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class EnemyManager : Microsoft.Xna.Framework.GameComponent
    {
        private static EnemyManager enemyManager = null;
        public List<StaticEnemy> enemy_list = new List<StaticEnemy>();
        private List<Missile> missile_list = new List<Missile>();
        static int max_enemies = 20;
        Random random_num = new Random();
        Player player;
        Planet planet;
        Utils.Timer enemy_spawner;

        public List<Missile> MissileList
        {
            get
            {
                return missile_list;
            }
        }

        public EnemyManager(Game game_, Player player_, Planet planet_)
            : base(game_)
        {
            // TODO: Construct any child components here
            player = player_;
            planet = planet_;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            float magnitudeRatio = CheckpointManager.MinRadiusMagnitude + ((float)random_num.NextDouble() * (CheckpointManager.MaxRadiusMagnitude - CheckpointManager.MinRadiusMagnitude));
            StaticEnemy temp = new StaticEnemy(Game, player, magnitudeRatio, 0.2f, missile_list, planet);
            enemy_list.Add(temp);
            Game.Components.Add(temp);
            enemy_spawner = new Utils.Timer();
            enemy_spawner.AddTimer("spawn", 10, new Utils.TimerDelegate(spawn_enemy), true);
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (!GameplayScreen.pause)
            {
                enemy_spawner.Update(gameTime);
                base.Update(gameTime);
            }
        }

        public void remove_enemy(StaticEnemy temp)
        {
            Game.Components.Remove(temp);
            enemy_list.Remove(temp);
        }

        public void remove_missile(Missile temp)
        {
            Game.Components.Remove(temp);
            missile_list.Remove(temp);
        }

        public void spawn_enemy()
        {
            float magnitudeRatio = CheckpointManager.MinRadiusMagnitude + ((float)random_num.NextDouble() * (CheckpointManager.MaxRadiusMagnitude - CheckpointManager.MinRadiusMagnitude));
            StaticEnemy temp = new StaticEnemy(Game, player, magnitudeRatio, 0.2f, missile_list, planet);
            enemy_list.Add(temp);
            Game.Components.Add(temp);
        }
    }
}
